package com.ft.adbix.core.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.ft.adbix.core.R;
import com.ft.adbix.core.listener.BannerListener;
import com.ft.adbix.core.response.Ad;
import com.ft.adbix.core.widget.InteractionLayout;
import com.ft.adbix.utils.AdLogUtils;
import com.ft.adbix.utils.AdOpener;
import com.ft.adbix.utils.AdTrackerReporter;
import com.ft.adbix.utils.universalimageloader.core.ImageLoader;

public class BannerAdView extends FrameLayout {
    private TextView tvTitle, tvDesc;
    private Ad adData;
    private ImageView ivPic, ivClose;
    private InteractionLayout layoutInteraction;
    private BannerListener listener;
    private AdOpener adOpener;
    private AdTrackerReporter reporter;
    //0：左图右文  1：全图
    private int bannerStyle = 0;
    public static final int BANNER_STYLE_LEFT_PIC=0;
    public static final int BANNER_STYLE_PIC=1;

    public BannerAdView(@NonNull Context context, int bannerStyle) {
        this(context, null);
        this.bannerStyle = bannerStyle;
    }

    public BannerAdView(@NonNull Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public BannerAdView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

    }

    public void renderView(Ad adData, BannerListener listener) {
        this.adData = adData;
        this.listener = listener;
        reporter = new AdTrackerReporter(adData);
        adOpener = new AdOpener(getContext(), adData, reporter);

        if (bannerStyle==0){
            createLeftPicView();
        }else{
            createAllPicView();
        }

    }

    private void createLeftPicView() {
        LayoutInflater.from(getContext()).inflate(R.layout.adbix_banner_template_style_left_pic, this);
        tvTitle = findViewById(R.id.adbix_banner_tv_title);
        tvDesc = findViewById(R.id.adbix_banner_tv_desc);
        ivPic = findViewById(R.id.adbix_banner_iv_pic);
        ivClose = findViewById(R.id.adbix_banner_iv_close);
        layoutInteraction = findViewById(R.id.adbix_banner_layout_interaction);layoutInteraction.setReporter(reporter);
        tvTitle.setText(adData.getTitle());
        tvDesc.setText(adData.getDesc());
        ivClose.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listener != null) {
                    listener.onAdClose();
                }
            }
        });
        layoutInteraction.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                AdLogUtils.d("click = " + reporter.getMarkInfo());
                if (listener != null) {
                    listener.onAdClick();
                }
                reporter.reportClick();
                adOpener.openAd();
            }
        });

        renderPic();

    }

    private void createAllPicView() {
        LayoutInflater.from(getContext()).inflate(R.layout.adbix_banner_template_style_pic, this);
        ivPic = findViewById(R.id.adbix_banner_iv_pic);
        ivClose = findViewById(R.id.adbix_banner_iv_close);
        layoutInteraction = findViewById(R.id.adbix_banner_layout_interaction);layoutInteraction.setReporter(reporter);
        ivClose.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listener != null) {
                    listener.onAdClose();
                }
            }
        });
        layoutInteraction.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                AdLogUtils.d("click = " + reporter.getMarkInfo());
                if (listener != null) {
                    listener.onAdClick();
                }
                reporter.reportClick();
                adOpener.openAd();
            }
        });

        renderPic();

    }

    private void renderPic() {
//        if (adData.getImageUrlList() != null && adData.getImageUrlList().length != 0) {
//           //轮播图
//        } else {
        //单图
//            ImageLoader.getInstance().displayImage(adData.getImageUrl(), ivPic);
        ImageLoader.getInstance().displayImage("https://doc.admobile.top/ADimages/ceshi/flow3.jpg", ivPic);
        if (listener != null) {
            listener.onRenderSuccess();
        }
//        }
    }

    @Override
    protected void onAttachedToWindow() {
        super.onAttachedToWindow();
        reporter.reportDisplay();
        if (listener != null) {
            listener.onAdShow();
        }
    }

}
